package com.astar.exception;

import com.astar.enums.ReturnCodeEnum;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 自定义全局异常处理器，所有的异常都在这里处理
 * Created by liuruiyan on 2019/3/27.
 */

//@Component
public class CustomExceptionResolver implements HandlerExceptionResolver {

    @Override
    public ModelAndView resolveException(HttpServletRequest httpServletRequest,
                                         HttpServletResponse httpServletResponse, Object handler, Exception e) {
        //打印错误信息
        e.printStackTrace();

        String message = null;
        if(e instanceof CustomException){
            message = e.getMessage();
        }else{
            message = ReturnCodeEnum.CODE_9999.getMsg();
        }


        CustomException customException = new CustomException(message);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("customException",customException);
        modelAndView.setViewName("error");

        return modelAndView;
    }
}
